package com.springboot.others.mapper;

import com.springboot.others.entity.Customer;
import com.springboot.others.entity.dto.PageCustomerDTO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CustomerMapper {
    @Select("select * from customer where customer_id = #{id}")
    Customer selectById(Integer id);

    @Insert("insert into customer values (null,#{storeId},#{firstName},#{lastName},#{email},#{addressId}," +
            "#{active},#{createDate},#{lastUpdate})")
    @Options(useGeneratedKeys = true,keyProperty = "customerId")
    void add(Customer customer);

    @Delete("delete from customer where customer.customer_id = #{id}")
    void del(Integer id);

    void update(Customer customer);

    List<Customer> selectAll();


    List<Customer> listByCondition(PageCustomerDTO pageCustomerDTO);

    /**
     * 通过customerId获取addressId
     * @param customerId
     * @return
     */
    @Select("select address_id from customer where customer_id = #{customerId}")
    Integer getAddressIdByCustomerId(Integer customerId);

    /**
     * 判断当前用户的地址是否有其他客户使用
     * @param addressId
     * @return
     */
    @Select("select count(customer_id) from customer where address_id = #{addressId}")
    int countAddressByAddressId(Integer addressId);
}
